#include "Character.h"

Character::Character() :
GameObject()
{
	m_timeCol = 0;
	m_accY = -4;
	m_isFire = false;
	isRight(true);
	setCFire(CF_RIGHT);
	setCPos(CP_FIRST);
	setCState(CS_NONE);
	setGameObID(CHARACTER);
	setPostion(ResourceManager::getInstance()->m_posChar);
	
	init();
}

Character::~Character()
{

}

void Character::init()
{
	m_sprite = ResourceManager::getInstance()->cS_character;
	m_input = ResourceManager::getInstance()->m_input;

	setWidth(m_sprite->getWidthTiled());
	setHeight(m_sprite->getHeightTiled());
}

void Character::processInput()
{
	// down arrow
	if (m_input->KeyDown(DIK_DOWNARROW))
	{
		isChangeAni(true);
		setCState(CS_DOWN);

	}
	if (m_input->onKeyUp(DIK_DOWNARROW))
	{
		resetState();
	}

	// up arrow
	if (m_input->KeyDown(DIK_UPARROW))
	{
		isChangeAni(true);
		setCState(CS_UP);
	}
	if (m_input->onKeyUp(DIK_UPARROW))
	{
		resetState();
	}

	// right arrow
	if (m_input->KeyDown(DIK_RIGHTARROW))
	{
		m_vecX = 5;
		isChangeAni(true);
		setCState(CS_RUNRIGHT);
		// add up arrow
		if (m_input->KeyDown(DIK_UPARROW))
		{
			setCState(CS_RUNUPRIGHT);
		}
		// add down arrow
		if (m_input->KeyDown(DIK_DOWNARROW))
		{
			setCState(CS_RUNDOWNRIGHT);
		}
	}
	if (m_input->onKeyUp(DIK_RIGHTARROW))
	{
		resetState();
	}

	// left arrow
	if (m_input->KeyDown(DIK_LEFTARROW))
	{
		m_vecX = -5;
		setCState(CS_RUNLEFT);
		isChangeAni(true);
		// add up arrow
		if (m_input->KeyDown(DIK_UPARROW))
		{
			setCState(CS_RUNUPLEFT);
		}
		// add down arrow
		if (m_input->KeyDown(DIK_DOWNARROW))
		{
			setCState(CS_RUNDOWNLEFT);
		}
	}
	if (m_input->onKeyUp(DIK_LEFTARROW))
	{
		resetState();
	}

	// Key Z
	if (m_input->onKeyDown(DIK_Z))
	{
		if (getCPos() != CP_AIR)
		{
			isFire(true);
			setCState(CS_FIRE);
		}
	}
	if (m_input->onKeyUp(DIK_Z))
	{
		//	isChangeAni(true);
	}

	// Key X
	if (m_input->onKeyDown(DIK_X))
	{
		if (getCPos() == CP_GROUND)
		{
			setVecY(7);
			isChangeAni(true);
			setCState(CS_JUMP);
			setCPos(CP_AIR);
		}

	}

}
void Character::update(float time)
{
	m_sprite->update();
	m_posX_old = m_posX;
	m_posY_old = m_posY;


	updateVec(time);
	setRectCollsion();
	colWithObject(time);
	processPostion();
	if (!isFire())
	{
		setAnimation();
	}
	setFireBullet();
	moveEnity();

	// update toa do nhan vat
	ResourceManager::getInstance()->m_posChar = D3DXVECTOR2(m_posX, m_posY);
}

void Character::setRectCollsion()
{
	setBotDeviation(-1);	
}

void Character::colWithObject(float time)
{
	vector<GameObject*> m_listObinView = ResourceManager::getInstance()->listObinView;
	for (int i = 0; i < m_listObinView.size(); i++)
	{
		if (m_listObinView.at(0)->getGameObID() == BG_GROUND)
		{
			
			DirectCollision dirGround = this->AABBCollision(m_listObinView.at(0), m_timeCol);
			if (dirGround == BOTTOM)
			{
				m_posY += m_timeCol*m_vecY;
				setCPos(CP_GROUND);
			}
			else if (dirGround == NONE)
			{				
				setCPos(CP_AIR);
			}
			
		}
	}

	

}

void Character::setAnimation()
{
	if (isChangeAni())
	{
		switch (m_CState)
		{
		case CS_NONE:
			if (isRight())
			{
				setCFire(CF_RIGHT);
			}
			else
			{
				setCFire(CF_LEFT);
			}
			if (m_sprite->getIndex() != 36)
			{
				m_sprite->animate(2, 36, 36);
			}
			break;
		case CS_RUNRIGHT:
			isRight(true);
			if (m_sprite->getIndex() < 10 || m_sprite->getIndex() > 14)
			{
				m_sprite->animate(2, 10, 14);
				setCFire(CF_RIGHT);
			}
			break;
		case CS_RUNLEFT:
			isRight(false);
			if (m_sprite->getIndex() < 10 || m_sprite->getIndex() > 14)
			{
				m_sprite->animate(2, 10, 14);
				setCFire(CF_LEFT);
			}
			break;
		case CS_UP:
			if (m_sprite->getIndex() != 37)
			{
				m_sprite->animate(2, 37, 37);
				setCFire(CF_UP);
			}
			break;
		case CS_RUNUPRIGHT:
			isRight(true);
			if (m_sprite->getIndex() < 4 || m_sprite->getIndex() > 6)
			{
				m_sprite->animate(2, 4, 6);
				setCFire(CF_UPRIGHT);
			}
			break;
		case CS_RUNUPLEFT:
			isRight(false);
			if (m_sprite->getIndex() < 4 || m_sprite->getIndex() > 6)
			{
				m_sprite->animate(2, 4, 6);
				setCFire(CF_UPLEFT);
			}
			break;
		case CS_DOWN:
			if (m_sprite->getIndex() != 38)
			{
				m_sprite->animate(2, 38, 38);
				setCFire(CF_DOWN);
			}
			break;
		case CS_RUNDOWNRIGHT:
			isRight(true);
			if (m_sprite->getIndex() < 7 || m_sprite->getIndex() > 9)
			{
				m_sprite->animate(2, 7, 9);
				setCFire(CF_DOWNRIGHT);
			}
			break;
		case CS_RUNDOWNLEFT:
			isRight(false);
			if (m_sprite->getIndex() < 7 || m_sprite->getIndex() > 9)
			{
				m_sprite->animate(2, 7, 9);
				setCFire(CF_DOWNLEFT);
			}
			break;
		case CS_JUMP:
			if (m_sprite->getIndex() < 15 || m_sprite->getIndex() > 18)
			{
				m_sprite->animate(1, 15, 18);
			}
			break;
		case CS_FIRE:
			break;

		default:
			break;
		}
	}
}

void Character::setFireBullet()
{
	if (isFire())
	{
		switch (m_CFire)
		{
		case CF_RIGHT:
			if (m_sprite->getIndex() < 0 || m_sprite->getIndex() > 1)
			{
				m_sprite->animate(1, 0, 1, 1, 36);
			}
			break;
		case CF_LEFT:
			if (m_sprite->getIndex() < 0 || m_sprite->getIndex() > 1)
			{
				m_sprite->animate(1, 0, 1, 1, 36);
			}
			break;
		case CF_DOWN:
			if (m_sprite->getIndex() < 24 || m_sprite->getIndex() > 25)
			{
				m_sprite->animate(1, 23, 25, 1, 38);
			}
			break;
		case CF_DOWNRIGHT:
			
			break;
		case CF_DOWNLEFT:
			
			break;
		case CF_UP:
			if (m_sprite->getIndex() < 2 || m_sprite->getIndex() > 3)
			{
				m_sprite->animate(1, 2, 3, 1, 37);
			}
			break;
		case CF_UPRIGHT:
			break;
		case CF_UPLEFT:
			break;
		default:
			break;
		}
		isFire(false);

	}

}

void Character::processPostion()
{
	switch (m_CPos)
	{
	case CP_AIR:
		if (getCState() != CS_JUMP)
		{
			isChangeAni(false);
			setCState(CS_JUMP);
		}

		break;
	case CP_GROUND:
		if (getCState() == CS_JUMP)
		{
			isChangeAni(true);
			setCState(CS_NONE);
		}
		m_vecY = 0;
		break;
	case CP_WATER:
		break;
	default:
		break;
	}
}

void Character::resetState()
{

	if (getCFire() != CF_NONE)
	{
		isChangeAni(true);
		setCState(CS_NONE);
	}
	stopVec();
}

void Character::counterVec(float vecX, float  vecY)
{
	m_vecX -= vecX;
	m_vecY -= vecY;
}

void Character::draw()
{

	D3DXVECTOR2 pos = m_camera->getPointTransform(m_posX, m_posY);
	if (isRight())
	{
		m_sprite->draw(m_spriteHandler, pos.x, pos.y, false);
	}
	else
	{
		m_sprite->draw(m_spriteHandler, pos.x, pos.y, true);
	}

}

void Character::destroy()
{

}


